package com.hsc.www.validator.test2;

import com.hsc.www.validator.test3.User;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StudentController {

    @PostMapping("/addStudent")
    public String addStudent(@RequestBody @Validated(value = {Insert.class}) User user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuffer stringBuffer = new StringBuffer();
            for (ObjectError error : bindingResult.getAllErrors()) {
                stringBuffer.append(error.getDefaultMessage()).append(";");
            }
            return stringBuffer.toString();
        } else {
            return user.toString();
        }
    }

    @PostMapping("/updateStudent")
    public String updateStudent(@RequestBody @Validated(value = {Update.class}) User user, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            StringBuffer stringBuffer = new StringBuffer();
            for (ObjectError error : bindingResult.getAllErrors()) {
                stringBuffer.append(error.getDefaultMessage()).append(";");
            }
            return stringBuffer.toString();
        } else {
            return user.toString();
        }
    }


}